<template>
  <div id="table_container">
    <el-card style="margin-bottom: 5px; text-align: left;">
      <el-input v-model="keyWord" placeholder="输入搜索内容" style="width: 200px;" @keyup.enter.native="handleFilter"></el-input>
      <el-select v-model="status" placeholder="状态" style="width: 90px;margin-left: 10px;">
        <el-option v-for="item in importanceOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="handleFilter" style="margin-left: 10px;">
        查询
      </el-button>
      <el-button v-permission="['batch:add']" class="create_btn" style="float: right;" type="primary" icon="el-icon-edit" @click="handleCreate">
        新增
      </el-button>
    </el-card>
    <el-card>
      <el-table
          :data="tableData"
          v-loading="loading"
          row-key="id"
          border>
        <el-table-column
            type="selection"
            align="center"
            width="48">
        </el-table-column>
        <el-table-column
            type="index"
            label="序号"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="number"
            width="100"
            align="center"
            label="筛查批号">
        </el-table-column>
        <el-table-column
            prop="total"
            label="总人数">
        </el-table-column>
        <el-table-column
            prop="school_label"
            label="学校">
        </el-table-column>
        <el-table-column
            prop="hospital_label"
            label="医院">
        </el-table-column>
        <el-table-column
            label="结束时间"
            align="center"
            width="120">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.end_time }}</span>
          </template>
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态"
            align="center"
            width="70">
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.status === true ? 'success' :'danger' "
                disable-transitions>{{ scope.row.status === true ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="创建时间"
            align="center"
            width="120">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.create_time }}</span>
          </template>
        </el-table-column>
        <el-table-column
            fixed="right"
            width="180"
            align="center"
            label="操作">
          <template slot-scope="scope">
            <el-button v-permission="['batch:edit']" @click="handleEdit(scope.row)" title="编辑" type="primary" icon="el-icon-edit" size="small">编辑</el-button>
            <el-button v-permission="['batch:delete']" @click="handleDelete(scope.row)" title="删除" type="danger" icon="el-icon-delete" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          v-show="pageTotal>0"
          :total="pageTotal"
          :page.sync="page"
          :pageSize.sync="pageSize"
          @pagination="getDataList"/>
    </el-card>

    <el-dialog title="新增计划" width="400px" @close="handleClose" :visible.sync="createDialogFormVisible">
      <el-form :model="form">

        <el-form-item label="筛查学校" :label-width="formLabelWidth">
          <el-select v-model="form.school" placeholder="请选择" filterable style="width: 100%">
            <el-option
                v-for="item in schoolOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学期" :label-width="formLabelWidth">
          <el-input v-model="form.semester" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="计划人数" :label-width="formLabelWidth">
          <el-input v-model.number="form.total" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="负责医院" :label-width="formLabelWidth">
          <el-select v-model="form.hospital" placeholder="请选择" filterable style="width: 100%">
            <el-option
                v-for="(item,index) in hospitalOptions"
                :key="index"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="结束时间" :label-width="formLabelWidth">
          <el-date-picker
              style="width: 100%"
              v-model="form.end_time"
              type="date"
              placeholder="不建议填写">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="createDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitCreate">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="查看信息" width="400px" @close="handleClose" :visible.sync="showDialogFormVisible">
      <el-form :model="form" disabled>

        <el-form-item label="筛查学校" :label-width="formLabelWidth">
          <el-select v-model="form.school" placeholder="请选择" filterable style="width: 100%">
            <el-option
                v-for="item in schoolOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学期" :label-width="formLabelWidth">
          <el-input v-model="form.semester" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="计划人数" :label-width="formLabelWidth">
          <el-input v-model.number="form.total" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="负责医院" :label-width="formLabelWidth">
          <el-select v-model="form.hospital" placeholder="请选择" filterable style="width: 100%">
            <el-option
                v-for="(item,index) in hospitalOptions"
                :key="index"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="结束时间" :label-width="formLabelWidth">
          <el-date-picker
              style="width: 100%"
              v-model="form.end_time"
              type="date"
              placeholder="不建议填写">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer"></div>
    </el-dialog>
    <el-dialog title="编辑信息" width="400px" @close="handleClose" :visible.sync="editDialogFormVisible">
      <el-form :model="form">

        <el-form-item label="筛查学校" :label-width="formLabelWidth">
          <el-select v-model="form.school" placeholder="请选择" filterable style="width: 100%">
            <el-option
                v-for="item in schoolOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学期" :label-width="formLabelWidth">
          <el-input v-model="form.semester" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="计划人数" :label-width="formLabelWidth">
          <el-input v-model.number="form.total" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="负责医院" :label-width="formLabelWidth">
          <el-select v-model="form.hospital" placeholder="请选择" filterable style="width: 100%">
            <el-option
                v-for="(item,index) in hospitalOptions"
                :key="index"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="结束时间" :label-width="formLabelWidth">
          <el-date-picker
              style="width: 100%"
              v-model="form.end_time"
              type="date"
              placeholder="不建议填写">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
              type="textarea"
              :autosize="{ minRows: 4, maxRows: 8}"
              placeholder="请输入内容"
              v-model="form.description">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>


<script>
import {createBatch, createUser, deleteBatch, getBatch, getDept, updateBatch, updateUser} from "@/api";
import pagination from "@/components/Pagenation";

export default {
  name: "index",
  components: {
    pagination
  },
  data() {
    return {
      tableData: [],
      loading: true,
      keyWord: undefined,
      importanceOptions: [{label: '启用', value: 1}, {label: '停用', value: 0}],
      status: undefined,

      // 分页器相关
      page: 1,
      pageTotal: 0,
      pageSize: 10,

      formLabelWidth: '80px',
      createDialogFormVisible: false,
      showDialogFormVisible: false,
      editDialogFormVisible: false,
      form: {
        number: undefined,
        semester: undefined,
        total: undefined,
        school: undefined,
        hospital: undefined,
        description: undefined,
        end_time: undefined,
        status: true
      },
      hospitalOptions: [],
      schoolOptions: [],

    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    getDataList(params = {}) {
      this.loading = true

      params['page'] = this.page
      params['pageSize'] = this.pageSize
      params['keyWord'] = this.keyWord
      params['status'] = this.status
      getBatch(params).then(res => {
        this.tableData = res.data.results;
        this.pageTotal = res.data.count;
        setTimeout(() => {
          this.loading = false
        }, 1.5 * 500)
      })
    },
    handleClose() {
      this.form = {
        name: undefined,
        password: undefined,
        id_card: undefined,
        phone: undefined,
        email: undefined,
        gender: 0,
        roles: undefined,
        posts: undefined,
        dept: undefined,
      }

    },
    handleDelete(row) {
      deleteBatch(row.id).then(res => {
        this.$message.success('删除成功')
        this.getDataList()
      })
    },
    handleFilter() {
      this.getDataList()
    },
    handleCreate() {
      this.createDialogFormVisible = true
      this.loadDialogOptions()
    },
    handleDetail(row) {
      this.showDialogFormVisible = true
      this.loadDialogOptions()
      this.form = {...row}
    },
    handleEdit(row) {
      this.editDialogFormVisible = true
      this.loadDialogOptions()
      this.form = {...row}
    },
    loadDialogOptions() {
      getDept({deptType: 0}).then(res => {
        this.schoolOptions = res.data
      });
      getDept({deptType: 1}).then(res => {
        this.hospitalOptions = res.data
      });
    },
    submitCreate() {
      let data = this.form
      createBatch(data).then(res => {
        this.$message.success('新建成功！')
        this.createDialogFormVisible = false
        this.getDataList()
      })
    },
    submitUpdate() {
      let data = this.form
      updateBatch(data).then(res => {
        this.$message.success('修改成功！')
        this.editDialogFormVisible = false
        this.getDataList()
      })
    },

  }

}

</script>

<style scoped>

</style>